package cn.easytogo.action.admin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.easytogo.action.BaseAction;
import cn.easytogo.bean.Resource;
import cn.easytogo.service.IResourceDAO;

public class ResourceAction  extends BaseAction{
	private IResourceDAO resourceDAO;
	private String id;
	private String level;
	private String pid;
	private String url;
	private String name;
	private String admin;
	private int dirc;//移动方向
	/**
	 * 前往功能列表维护页面
	 * */
	public String toList(){
		return "to_list";
	}
	/**
	 * 前往功能新加页面
	 * */
	public String toNew(){
		return "to_new";
	}
	/**
	 * 添加新功能
	 * */
	public void saveNew(){
		if(pid!=null&&!"".equals(pid)&&name!=null&&!"".equals(name)&&level!=null&&!"".equals(level)){
			Resource resourceToInsert = new Resource();
			resourceToInsert.setAdmin(admin);
			resourceToInsert.setLevel(level);
			resourceToInsert.setName(name);
//			serviceToInsert.setOrderCode(order);
			resourceToInsert.setPid(pid);
			resourceToInsert.setUrl(url);
			try {
				Integer id = (Integer) resourceDAO.insertResource(resourceToInsert);
				print(id.toString());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 前往功能修改页面
	 * */
	public String toUpdate(){
		Map map = new HashMap();
		map.put("id", id);
		try {
			List l = resourceDAO.selectResources(map);
			if(l!=null&&l.size()>0){
				Map resourceMap = (Map) l.get(0);
				name=(String) resourceMap.get("name");
				url=(String) resourceMap.get("url");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_new";
	}
	
	/**
	 * 更新功能
	 * */
	public void saveUpdate(){
		if(id!=null&&!"".equals(id)&&name!=null&&!"".equals(name)){
			Resource resourceToUpdate = new Resource();
			resourceToUpdate.setId(id);
			resourceToUpdate.setName(name);
			resourceToUpdate.setUrl(url);
			try {
				resourceDAO.updateResource(resourceToUpdate);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 删除功能
	 * */
	public void delete(){
		if(id!=null&&!"".equals(id)){
			Resource resourceToDelete = new Resource();
			resourceToDelete.setId(id);
			try {
				resourceDAO.deleteResource(resourceToDelete);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 移动功能
	 * */
	public void move(){
		if(id==null||"".equals(id)){
			print("failed");
			return;
		}
		try{
			switch (dirc) {
				case IResourceDAO.MOVE_DIRECTION_UP:
					moveUpOrDown(dirc);
				break;
				case IResourceDAO.MOVE_DIRECTION_DOWN:
					moveUpOrDown(dirc);
				break;
				case IResourceDAO.MOVE_DIRECTION_LEFT:
					moveLeftOrRight(dirc);
				break;
				case IResourceDAO.MOVE_DIRECTION_RIGHT:
					moveLeftOrRight(dirc);
				break;
			}
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			print("failed");
		}
	}
	
	/**
	 * 上下移动处理方法
	 * @throws SQLException 
	 * */
	private void moveUpOrDown(int dirc) throws SQLException{
		Map map = new HashMap();
		map.put("id", id);
		map.put("dirc", dirc);
		map.put("pid", pid);
		Resource r1 = resourceDAO.selectResourceById(map);//获取要移动的功能
		Resource r2 = resourceDAO.selectResourceByOrderCode(map);//获取移动后所在位置的现在的功能
		String orderCodeTemp = r1.getOrderCode();//临时将r1的排序号存放起来
		r1.setOrderCode(r2.getOrderCode());//将r2排序号给r1
		r2.setOrderCode(orderCodeTemp);//将事先保存的r1排序号给r2
		List resources = new ArrayList();
		resources.add(r1);
		resources.add(r2);
		resourceDAO.updateResources(resources);
	}
	/**
	 * 左右移动处理方法
	 * @throws SQLException 
	 * */
	private void moveLeftOrRight(int dirc) throws SQLException{
		Resource resource = new Resource();
		resource.setId(id);
		resource.setLevel(level);
		resource.setPid(pid);
		resourceDAO.updateResource(resource);
	}
	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAdmin() {
		return admin;
	}
	public void setAdmin(String admin) {
		this.admin = admin;
	}
	public IResourceDAO getResourceDAO() {
		return resourceDAO;
	}
	public void setResourceDAO(IResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}
	public int getDirc() {
		return dirc;
	}
	public void setDirc(int dirc) {
		this.dirc = dirc;
	}
	
}
